﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Systems;
using PH.Logic.Systems;
namespace PH.Web.Management.Areas.Systems.Controllers
{
    [RouteArea("Systems")]
    [RoutePrefix("Bureau")]
    public class BureauApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<BureauModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                var getUser = request.Value<bool>("getUser");
                var widthme = request.Value<bool>("withme");
                var sguid = request.Value<string>("guid");
                Guid guid;
                if (!string.IsNullOrEmpty(sguid))
                    guid = new Guid(sguid);
                else
                    guid = Web.Global.WebContext.CurrentBureau.Guid;
                var ret = BureauLogic.GetChildren(guid, getUser,widthme);
                return new JsonEntitySet<BureauModel>(ret);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<BureauModel>(null, errinfo, false);
        }

        //[Route("GetChidrenWithMe")]
        //[HttpPost]
        //public JsonEntitySet<BureauModel> GetChidrenWithMe(JObject request)
        //{
        //    string errinfo = "no data";
        //    try
        //    {
        //        var ret = BureauLogic.GetChildrenWithMe(Web.Global.WebContext.CurrentBureau.Guid);
        //        return new JsonEntitySet<BureauModel>(ret);
        //    }
        //    catch (Exception ex)
        //    {
        //        errinfo = ex.Message;
        //        Common.Logger.Error("", ex);
        //    }
        //    return new JsonEntitySet<BureauModel>(null, errinfo, false);
        //}

        [Route("GetAllChidren")]
        [HttpPost]
        public JsonEntitySet<BureauModel> GetAllChidren(JObject request)
        {
            string errinfo = "no data";
            try
            {
                var withme = request.Value<bool>("withme");
                var ret = BureauLogic.GetAllChildren(Web.Global.WebContext.CurrentBureau.Guid,withme);
                return new JsonEntitySet<BureauModel>(ret);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<BureauModel>(null, errinfo, false);
        }

        [Route("Save")]
        [HttpPost]
        public JsonResult<bool> Save(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                var model = new BureauModel();
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                    model.Guid = new Guid(guid);
                model.EduName = request.Value<string>("eduName");
                model.Code = request.Value<string>("code");
                model.PID = request.Value<int>("pID");
                #endregion

                if (model.Guid != Guid.Empty)
                    BureauLogic.Update(model);
                else
                    BureauLogic.CreateNew(model);

                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("Delete")]
        [HttpPost]
        public JsonResult<bool> Delete(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var sguid = request.Value<string>("guid");
                var guid = new Guid(sguid);
                #endregion

                BureauLogic.Delete(guid);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

    }
}
